package swu.cx.progressbar

import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
class ProgressView: View {
    //定义变量保存下载结果
    var result = UNKNOW
    //1.定义函数的类型
    // 参数 就是需要返回的数据的类型
    // 返回值 是否需要对⽅给我结果
    var callBack: (() -> Unit)? = null

    //定义变成半圆的动画因⼦
    private var cornerRadius = 0f

    //定义中间靠拢的动画因⼦
    private var tranx = 0f

    //定义进度的变化因⼦ 0-1.0
    var progress = 0f
        set(value) {
            field = value
            //刷新
            invalidate()
            //开启完成动画
            if (progress == 1.0f) {
                startFinishAnim()
            }
        }

    //矩形区域的画笔
    private val mRoundRectPaint = Paint().apply {
        color = Color.BLUE
        style = Paint.Style.FILL
    }

    //绘制勾勾或者叉叉的路径Path
    private val markPath = Path()

    //结果的size
    private var markSize = 0f

    //中⼼点坐标
    private var cx = 0f
    private var cy = 0f

    //勾勾裁剪动画的动画因⼦
    private var clipSize = 0f

    //勾勾叉叉的画笔
    private val markPaint = Paint().apply {
        color = Color.WHITE
        strokeWidth = 10f
        style = Paint.Style.STROKE
    }

    //暴露接⼝给外部使⽤
    var bgColor: Int = Color.BLUE

    //静态变量 ⽅法
    companion object {
        val SUCCESS = 0
        val FAILUE = 1
        val UNKNOW = 2
    }

    constructor(context: Context) : super(context) {}
    constructor(
        context:
        Context, attrs: AttributeSet?
    ) : super(context, attrs) {
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        //勾勾或者叉叉的矩形边⻓
        markSize = height / 3f
        //中⼼点坐标
        cx = width / 2f
        cy = height / 2f
    }

    override fun onDraw(canvas: Canvas?) {
        //背景
        if (progress < 1.0) {
            mRoundRectPaint.color = bgColor
            canvas?.drawRoundRect(
                0f, 0f, width.toFloat(), height.toFloat(),
                0f, 0f,
                mRoundRectPaint
            )
        }
        //进度
        mRoundRectPaint.color = Color.MAGENTA
        canvas?.drawRoundRect(
            tranx, 0f, progress * width - tranx, height.toFloat(),
            cornerRadius, cornerRadius,
            mRoundRectPaint
        )
        //判断是否需要结果
        if (tranx == (width - height) / 2f) {
            if (result == SUCCESS || result == UNKNOW) {
                //绘制勾勾
                markPath.apply {
                    moveTo(cx - markSize / 2, cy)
                    lineTo(cx - markSize / 8, cy + markSize / 2)
                    lineTo(cx + markSize / 2, cy - markSize / 4)
                }
            } else {
                //绘制叉叉
                markPath.apply {
                    moveTo(cx - markSize / 2, cy - markSize / 2)
                    lineTo(cx + markSize / 2, cy + markSize / 2)
                    moveTo(cx - markSize / 2, cy + markSize / 2)
                    lineTo(cx + markSize / 2, cy - markSize / 2)
                }
            }
            canvas?.drawPath(markPath, markPaint)
            //绘制遮罩层
            canvas?.drawRect(
                cx - markSize / 2 - 10 + clipSize, cy - markSize /
                        2 - 10, cx + markSize / 2 + 10, cy + markSize / 2 + 10,
                mRoundRectPaint
            )
        }
    }

    //下载完毕 启动动画
    private fun startFinishAnim() {
        //变成半圆
        val toCircleAnim = ValueAnimator.ofFloat(0f, height / 2f).apply {
            duration = 1000
            addUpdateListener { anim ->
                cornerRadius = anim.animatedValue as Float
                invalidate()
            }
        }
        //中间靠拢的动画
        val moveToCenterAnim = ValueAnimator.ofFloat(0f, (width-height) / 2f).apply {
            duration = 1000
            addUpdateListener { anim ->
                tranx = anim.animatedValue as Float
                invalidate()
            }
        }
        //裁剪动画
        val clipAnim = ValueAnimator.ofFloat(0f, markSize + 20).apply {
            duration = 1000
            addUpdateListener { anim ->
                clipSize = anim.animatedValue as Float
                invalidate()
            }
        }
        AnimatorSet().apply {
            playSequentially(
                toCircleAnim,
                moveToCenterAnim, clipAnim
            )
            start()
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            //3.调⽤
            callBack?.let { back ->
                back()
            }
        }
        return true
    }
}
